//
//  SpeakViewController.m
//  UI_Project_A
//
//  Created by GH on 16/2/26.
//  Copyright © 2016年 GH. All rights reserved.
//

#import "SpeakViewController.h"
#import "SpeakTableViewCell.h"
#import "HandleData.h"
#import <UIImageView+WebCache.h>
#import "ModelOfSpeak.h"
#import <DKNightVersion.h>
#import <MJRefresh.h>

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface SpeakViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>

@property (nonatomic ,retain)UIView *viewOftitle;
@property (nonatomic ,retain)UITableView *tableview;
@property (nonatomic ,retain)UIImageView *imageOfHead;
@property (nonatomic ,retain)NSString *picurl;
@property (nonatomic ,retain)UIView *viewOftext;
@property (nonatomic ,retain)UILabel *labeltext;
@property (nonatomic ,retain)UILabel *labelOfFollow;
@property (nonatomic ,retain)UIButton *buttonOfBack;
@property (nonatomic ,retain)NSMutableArray *arrModel;
@property (nonatomic ,retain)UIView *viewOfField;
@property (nonatomic ,retain)UITextField *textfield;
@property (nonatomic ,assign)CGFloat height;
@property (nonatomic ,retain)UILabel *labelOftitle;
@property (nonatomic ,retain)NSString *strText;
@property (nonatomic ,assign)NSInteger number;


@end

@implementation SpeakViewController



- (void) dealloc {

    [_strText release];
    [_labelOftitle release];
    [_textfield release];
    [_viewOfField release];
    [_buttonOfBack release];
    [_labelOfFollow release];
    [_labeltext release];
    [_viewOftitle release];
    [_tableview release];
    [_imageOfHead release];
    [_picurl release];
    [_viewOftext release];
    [super dealloc];


}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.number = 0;
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createTableview];
    [self dataTableview];
    [self data];
   [self createtitleOfTop];
    [self createTextfield];
    [self createNotification];
    [self handleRefresh];
}

- (void)createNotification {

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    
    
    [self.tableview addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
    
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {

    CGFloat y = [[change objectForKey:@"new"] CGPointValue].y;

//    NSLog(@"%f",y);
    if (y <= (HEIGHT/3)) {
        self.viewOftitle.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:(y / (HEIGHT/3))];
    } else {
        self.viewOftitle.backgroundColor = [UIColor redColor];
    }
    if (y > (HEIGHT/3)) {
        self.labelOftitle.text = self.strText ;
    }else {
    self.labelOftitle.text = @"";
    }

}



#pragma 键盘出现
- (void)keyboardWillShow:(NSNotification *)aNotification
{

    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    int height = keyboardRect.size.height;

    
    [UIView animateWithDuration:0.25 animations:^{
        
        self.viewOfField.frame = CGRectMake(0, HEIGHT - height - 50, WIDTH, 50);
    }];

}


- (void)dataTableview {
    
     NSString *url1 = [NSString stringWithFormat:@"http://c.3g.163.com/newstopic/list/hotqa/%@/%ld-10.html",self.model.expertId,self.number];
    [HandleData handleFloewNewWithString:url1 Block:^(id result) {
        
        NSArray *arr = [result objectForKey:@"data"];
        for (NSDictionary *dicHotList in arr) {
            NSDictionary *dicQuestion = [dicHotList objectForKey:@"question"];
            
            ModelOfSpeak *model = [[ModelOfSpeak alloc] init];
            model.userName = [dicQuestion objectForKey:@"userName"];
            model.userHeadPicUrl = [dicQuestion objectForKey:@"userHeadPicUrl"];
            
            model.content  = [NSMutableArray array];
            
            [model.content addObject: [dicQuestion objectForKey:@"content"]];
            
            //answer
            NSDictionary *dicAnswer = [dicHotList objectForKey:@"answer"];
            
            model.specialistHeadPicUrl = [dicAnswer objectForKey:@"specialistHeadPicUrl"];
            model.specialistName = [dicAnswer objectForKey:@"specialistName"];
            [model.content addObject:[dicAnswer objectForKey:@"content"]];
            model.cTime = [dicAnswer objectForKey:@"cTime"];
            

            
            [self.arrModel addObject:model];
            
        }
        
        
        
        
    }];
    
    
    self.number = self.number + 10;

    [self.tableview reloadData];

}


- (void)data {
    
    self.arrModel = [NSMutableArray array];
    NSString *url = [NSString stringWithFormat:@"http://c.3g.163.com/newstopic/qa/%@.html",self.model.expertId];
  //GET http://c.3g.163.com/newstopic/list/hotqa/EX8635704607709333696/10-10.html
    
    
   
    [HandleData handleFloewNewWithString:url Block:^(id result) {

       NSDictionary *dic = [result objectForKey:@"data"];
        
        NSDictionary *dicExpert = [dic objectForKey:@"expert"];
        self.strText = [dicExpert objectForKey:@"description"];
        
        
        self.picurl = [dicExpert objectForKey:@"picurl"];

        [self.imageOfHead sd_setImageWithURL:[NSURL URLWithString:self.picurl]];
        
        self.labeltext.text = [dicExpert objectForKey:@"alias"];
        
        NSString *follow = [NSString stringWithFormat:@"- %@关注 -",[dicExpert objectForKey:@"concernCount"]];
        self.labelOfFollow.text = follow;

    }];
    
    
    [self.tableview reloadData];

}




#pragma 标题栏
- (void)createtitleOfTop {

    self.viewOftitle = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
    [self.view addSubview:self.viewOftitle];
    [self.viewOftitle release];
    self.viewOftitle.backgroundColor = [UIColor clearColor];
    
    
    
    self.labelOftitle = [[UILabel alloc] initWithFrame:CGRectMake( 70, 10, WIDTH - 100, 50)];
    [self.viewOftitle addSubview:self.labelOftitle];
    self.labelOftitle.textColor = [ UIColor whiteColor];
    
    
    //返回button
    
    self.buttonOfBack = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonOfBack.frame = CGRectMake(10, 10, 50, 50);
    [self.viewOftitle addSubview:self.buttonOfBack];
    [self.buttonOfBack setImage:[UIImage imageNamed:@"jiantou3"] forState:UIControlStateNormal];
    [self.buttonOfBack addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)back:(UIButton *)button {

[self dismissViewControllerAnimated:YES completion:^{
    
}];


}



- (void)createTextfield {

    
    self.viewOfField = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT - 65, WIDTH, 65)];
    [self.view addSubview:self.viewOfField];
    self.viewOfField.layer.masksToBounds = YES;
    //    self.viewOfField.layer.cornerRadius = 3;
    self.viewOfField.backgroundColor = [UIColor whiteColor];
    self.viewOfField.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithWhite:0.400 alpha:1.000]);
    
    
    
    self.textfield = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, WIDTH - 20, 50)];

    [self.viewOfField addSubview:self.textfield];
    self.textfield.placeholder = @"输入你的问题";
    self.textfield.borderStyle = 3;
    self.textfield.delegate = self;
    
    self.textfield.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithWhite:0.400 alpha:1.000]);


}

-(BOOL)textFieldShouldReturn:(UITextField *)textField {

    
    [UIView animateWithDuration:0.25 animations:^{
        
       self.viewOfField.frame = CGRectMake(0, HEIGHT - 65, WIDTH, 65);
        
    }];
    [textField resignFirstResponder];
    return YES;
}



- (void)createTableview {
    
    //headOfImageView
    
    self.imageOfHead = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]];
    [self.view addSubview:self.imageOfHead];
    [self.imageOfHead release];
    self.imageOfHead.frame = CGRectMake(0, 0, WIDTH, HEIGHT / 3);
    self.imageOfHead.backgroundColor = [UIColor redColor];
    
    
    
    //毛玻璃view
    self.viewOftext = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 3)];
    [self.imageOfHead addSubview:self.viewOftext];
    [self.imageOfHead release];

    UIBezierPath *backgroundPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, WIDTH, HEIGHT / 3 + 20) cornerRadius:5];
    
    self.viewOftext.layer.shadowPath = backgroundPath.CGPath;
    self.viewOftext.layer.shadowOpacity = 0.5;
    self.viewOftext.layer.shadowRadius = 0;
    self.viewOftext.layer.shadowColor = [UIColor blackColor].CGColor;
    
    
    //view上的text
    
    
    self.labeltext = [[UILabel alloc] initWithFrame:CGRectMake(50, HEIGHT/8, WIDTH - 100, 80)];
    [self.viewOftext addSubview:self.labeltext];
    [self.labeltext release];
    self.labeltext.textColor = [UIColor whiteColor];
    self.labeltext.numberOfLines = 0;
    
    
    
    self.labelOfFollow = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 2 - 60 , HEIGHT / 8 + 80, 120, 40)];
    self.labelOfFollow.textColor = [UIColor whiteColor];
    [self.viewOftext addSubview:self.labelOfFollow];
   [self.labelOfFollow release];
    
    
    
    

    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    [self.view addSubview:self.tableview];

    self.tableview.tableHeaderView = self.imageOfHead;
    
    [self.tableview release];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    [self.tableview registerClass:[SpeakTableViewCell class] forCellReuseIdentifier:@"pool"];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.arrModel.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    SpeakTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    ModelOfSpeak *model = [self.arrModel objectAtIndex:indexPath.row];
    [cell passModelWith:model];
    
      cell.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor],[UIColor colorWithWhite:0.247 alpha:1.000]);
    cell.labelOfAnswerName.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor colorWithWhite:0.689 alpha:1.000]);
    cell.labelOfAnswerContect.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor colorWithWhite:0.689 alpha:1.000]);

    
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    ModelOfSpeak * model = [self.arrModel objectAtIndex:indexPath.row];

    
    
    CGFloat h = [SpeakTableViewCell heightForCell:model];
    

    return h + 20;
}



- (void)handleRefresh {


    
    self.tableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        
            [self dataTableview];
        
        [self.tableview.mj_footer endRefreshing];
            [self.tableview reloadData];
    
        
    }];


    
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

        self.number = 0;
        [self dataTableview];
            [self.tableview.mj_header endRefreshing];
            [self.tableview reloadData];
           }];

    
    

}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
